package com.otp.service.impl;

import com.otp.exception.BusinessException;
import com.otp.pojo.entity.Question;
import com.otp.pojo.request.question.*;
import com.otp.repository.QuestionRepository;
import com.otp.service.QuestionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    private QuestionRepository questionRepository;

    @Override
    public List<Question> list() {
        return questionRepository.findAll();
    }

    @Override
    public List<Question> listByStatus(String status) {
        return questionRepository.findByStatus(status);
    }

    @Override
    public Question getById(Long id) {
        return questionRepository.findById(id)
                .orElseThrow(() -> new BusinessException("试题不存在"));
    }

    @Override
    public Question create(QuestionCreateRequest request) {
        // 验证试题类型
        if (!isValidType(request.getType())) {
            throw new BusinessException("试题类型必须为 SINGLE_CHOICE, TRUE_FALSE 或 TEXT");
        }
        // 验证单选题必须有选项
        if ("SINGLE_CHOICE".equals(request.getType()) && (request.getOptions() == null || request.getOptions().isEmpty())) {
            throw new BusinessException("单选题必须提供选项");
        }
        // 验证判断题答案
        if ("TRUE_FALSE".equals(request.getType()) && !isValidTrueFalseAnswer(request.getAnswer())) {
            throw new BusinessException("判断题答案必须为 true 或 false");
        }
        Question question = new Question();
        BeanUtils.copyProperties(request, question);
        question.setStatus("DRAFT");
        return questionRepository.save(question);
    }

    @Override
    public Question update(QuestionUpdateRequest request) {
        Question question = questionRepository.findById(request.getId())
                .orElseThrow(() -> new BusinessException("试题不存在"));
        // 验证试题类型
        if (!isValidType(request.getType())) {
            throw new BusinessException("试题类型必须为 SINGLE_CHOICE, TRUE_FALSE 或 TEXT");
        }
        // 验证单选题必须有选项
        if ("SINGLE_CHOICE".equals(request.getType()) && (request.getOptions() == null || request.getOptions().isEmpty())) {
            throw new BusinessException("单选题必须提供选项");
        }
        // 验证判断题答案
        if ("TRUE_FALSE".equals(request.getType()) && !isValidTrueFalseAnswer(request.getAnswer())) {
            throw new BusinessException("判断题答案必须为 true 或 false");
        }
        BeanUtils.copyProperties(request, question);
        return questionRepository.save(question);
    }

    @Override
    public Question publish(Long id) {
        Question question = questionRepository.findById(id)
                .orElseThrow(() -> new BusinessException("试题不存在"));
        if ("PUBLISHED".equals(question.getStatus())) {
            throw new BusinessException("试题已发布");
        }
        // 验证单选题选项
        if ("SINGLE_CHOICE".equals(question.getType()) && (question.getOptions() == null || question.getOptions().isEmpty())) {
            throw new BusinessException("单选题必须包含选项");
        }
        question.setStatus("PUBLISHED");
        return questionRepository.save(question);
    }

    @Override
    public void delete(Long id) {
        Question question = questionRepository.findById(id)
                .orElseThrow(() -> new BusinessException("试题不存在"));
        questionRepository.delete(question);
    }

    private boolean isValidType(String type) {
        return "SINGLE_CHOICE".equals(type) || "TRUE_FALSE".equals(type) || "TEXT".equals(type);
    }

    private boolean isValidTrueFalseAnswer(String answer) {
        return "true".equalsIgnoreCase(answer) || "false".equalsIgnoreCase(answer);
    }
}